#include "csvline.h"

 

CSVLine::CSVLine(QString line,QChar sep/*=','*/,QChar strQua/*='\"'*/ )
:err(false)
{	
	bool instr = false;
	QString nxt;
	for(int i = 0;i<line.size();i++) {
		if (line[i]==strQua) {
			i++;
			while (!(line[i]==strQua && i+1 < line.size() && line[i+1]!=strQua)) {//not reaching end of string 
				if (line[i]==strQua)
					nxt+=line[i++];
				nxt+=line[i++];
			}
			i++;
			lst.push_back(nxt);
			nxt="";
		} else if (line[i]==sep) {
			lst.push_back(nxt);
			nxt="";
		} else {
			nxt+=line[i];
		}
	}
	lst<<nxt;

}
	
CSVLine::~CSVLine()
{

}

 QString CSVLine::operator[]( int pos )
{
	if (pos<lst.size())
		return lst[pos];
	else return "";
}

 QString CSVLine::toString(char sep/*=','*/,char strqua /*= '\"'*/)
 {	
	QString ret;
	foreach(QString s,lst) {
		if (s.indexOf(sep)!=-1)
			ret+="\""+s+"\",";
		else ret+=s+",";
	}
	return ret;
 }
